/**
 * Created by sun on 2015/3/12.
 */
package com.jbm;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class GDDecoder extends CumulativeProtocolDecoder
{
//    private static final Logger LOGGER = Logger.getLogger(GDDecoder.class);

	private final String charset;

	public GDDecoder(String charset)
	{
		this.charset = charset;
	}

	protected boolean doDecode(IoSession session, IoBuffer in,
	                           ProtocolDecoderOutput out) throws Exception
	{
		if (in.remaining() >= 1)
		{
			in.mark();

			int cnt = in.remaining();

			int i = 0;

			for ( i = 0;i < cnt; i ++ )
			{
				byte b = in.get();

				if ( b == 0x0d )
				{
					byte[] msg = new byte[i+1];
					in.reset();
					in.get(msg);

					out.write(msg);

					return true;
				}
			}

			in.reset();
		}

		return false;// 处理成功，让父类进行接收下个包
	}
}
